﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Client.ControlHelper;
using System.Collections.Generic;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Content.NewsList {
    public partial class List : PageSupport,ICellulaBiz {
        public String ModelName = CodeTypeDef.Module.Newslist;
        INewslistBiz biz = BizFactory.Get<INewslistBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                LoadEOList();
            }
        }

        private void LoadEOList() {
            String hql = " from VNewsEditEO eo where eo.OwnerKey='" + this.OwnerKey + "' and eo.CategoryType='" + CodeTypeDef.Category.News + "' ";
            if (!String.IsNullOrEmpty(this.txt_Title.Text)) {
                hql += " and eo.Title like '%" + this.txt_Title.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.hid_ListType.Value)) {
                hql += " and eo.ListKey = '" + this.hid_ListType.Value + "' ";
            }
            if (!String.IsNullOrEmpty(this.txt_StartDate.Text)) {
                hql += " and eo.LastDate >= '" + this.txt_StartDate.Text + "' ";
            }
            if (!String.IsNullOrEmpty(this.txt_EndDate.Text)) {
                hql += " and eo.LastDate <= '" + this.txt_EndDate.Text + "' ";
            }
            if (!String.IsNullOrEmpty(this.ddl_Top.SelectedValue)) {
                hql += " and eo.FlgTop= " + this.ddl_Top.SelectedValue + " ";
            }
            if (!String.IsNullOrEmpty(this.ddl_Headline.SelectedValue)) {
                hql += " and eo.FlgHeadline= " + this.ddl_Headline.SelectedValue + " ";
            }
            if (!String.IsNullOrEmpty(this.ddl_IsVerified.Text)) {
                hql += " and eo.IsVerified= " + this.ddl_IsVerified.SelectedValue + " ";
            }
            hql += " order by eo.StampTime desc,eo.LastDate desc";
            IList<VNewsEditEO> list = biz.Find<VNewsEditEO>(hql);
            this.gvList.DataSource = list;
            this.gvList.DataBind();

        }

        private void InitForm() {
            ICodeBiz codeBiz = BizFactory.Get<ICodeBiz>();
            ClientBiz.BindYNList(ref this.ddl_Headline, false, TxtDef.DDLTextAll);
            ClientBiz.BindYNList(ref this.ddl_Top, false, TxtDef.DDLTextAll);
            this.txt_EndDate.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            this.txt_StartDate.Text = DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            LoadEOList();
        }

        protected void btn_EOAdd_Click(object sender, EventArgs e) {
            Response.Redirect("Edit.aspx");
        }

        protected void btn_EOUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                Response.Redirect("Edit.aspx?id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }

        }

        protected void btn_EODel_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.DeleteNews(keys);
                BizFactory.Get<ILogBiz>().LogDelete("新闻列表", keys.Count, this.Actor, Request);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
            LoadEOList();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvList.PageIndex = e.NewPageIndex;
            LoadEOList();
        }

        #region ICellulaBiz 成员

        string ICellulaBiz.ModelName {
            get { return CodeTypeDef.Module.Newslist; }
        }

        #endregion

        protected void btn_Verify_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.VerifyNews(keys);
                BizFactory.Get<ILogBiz>().Info("审核新闻列表", this.Actor, Request);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
            LoadEOList();
        }

        protected void btn_Top_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.SetTop(keys);
                BizFactory.Get<ILogBiz>().Info("置顶新闻列表", this.Actor, Request);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
            LoadEOList();
        }

        protected void btn_Headline_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.SetHeadline(keys);
                BizFactory.Get<ILogBiz>().Info("设置新闻列表头条", this.Actor, Request);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
            LoadEOList();
        }
    }
}
